import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class FailanaUnFail {
    public static void main(String[] args) {
        printQueue printQueue=new printQueue();
        Thread thread[]=new Thread[10];
        for (int i = 0; i <10 ; i++) {
            thread[i]=new Thread(new job(printQueue),"thread"+i);
        }
        for (int i = 0; i <10 ; i++) {
            thread[i].start();
            try {
                Thread.sleep(100);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

class job implements Runnable{

    private printQueue printQueue;

    public job(printQueue printQueue){
        this.printQueue=printQueue;
    }
    @Override
    public void run() {
        System.out.printf("%s:job thread",Thread.currentThread().getName());
        printQueue.prinfJob(new Object());
        System.out.printf("%s jobed",Thread.currentThread().getName());
    }
}

class printQueue{
    private final Lock queueLock=new ReentrantLock(true);
    public void prinfJob(Object dou){
        queueLock.lock();
        try {
           Long number=(long)Math.random()*10000;
            System.out.printf("%s:print:%dse",Thread.currentThread().getName(),(number/100));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            queueLock.unlock();
        }

        queueLock.lock();
        try {
            Long number=(long)Math.random()*10000;
            System.out.printf("%s:print:%dse",Thread.currentThread().getName(),(number/100));
            Thread.sleep(number);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            queueLock.unlock();
        }
    }
}
